#!/bin/bash
#*****************************************************************************
# Protected
# Copyright AUBASS CO., LTD.
#*****************************************************************************

show_help(){
    echo
    echo " -----------------------------------------------"
    echo "NAME"
    echo "      $(basename $0)"
    echo
    echo "SYNOPSIS"
    echo "      $(basename $0) [OPTION]... --docker-network-name=<name> --network-mask=<network> --ip-address=<IP> --bridge-name=<Bridge Name> --bind_mount(SET:Use Bind Mount, No SET:Use Volume control system))"
    echo
    echo "DESRIPTION"
    echo "      Create a machine and a new network of docker if necessary."
    echo
    echo "      --docker-network-name, -dnn"
    echo "          docker network name."
    echo
    echo "      --network-mask, -nm"
    echo "          network."
    echo
    echo "      --ip-address, -ia"
    echo "          adaptive application MACHINE IP Address."
    echo
    echo "      --sample-install, -saif"
    echo "          Sample Adaptive Application Installation flags (true = Installation : false = Not Installation)."
    echo
    echo "      --bridge-name, -bn"
    echo "          Network Bridge Name."
    echo
    echo "      --bind_mount, -bm"
    echo "          Use Bind Mount System flag."
    echo
    echo "EXAMPLE"
    echo "      $(basename $0) -dnn=someipdemos -nm=192.168.111.0/24 -ia=192.168.111.11 -saif=true -bn='br-aubist_ap' -bm"
    echo
    echo " -----------------------------------------------"

    exit 0
}

SAMPLE_INSTALL_FLAG=false
NETWORK_BN=""
BIND_FLG=0

source ${HOME}/.bash_profile
ret=$?
if [ 0 -ne ${ret} ]; then
    echo "Failed to load configuration file."
    exit ${ret}
fi

for i in $*; do
    case "$i" in
        -dnn=* | --docker-network-name=*)
            DOCKER_NETWORK_NAME=${i#-*=}
            ;;
        -nm=* | --network-mask=*)
            SUBNET=${i#-*=}
            ;;
        -ia=* | --ip-address=*)
            CONTAINER_IP=${i#-*=}
            ;;
        -saif=* | --sample-install=*)
            SAMPLE_INSTALL_FLAG=${i#-*=}
            ;;
        -bn=* | --bridge-name=*)
            NETWORK_BN="--opt com.docker.network.bridge.name=${i#-*=}"
            BRIDGE_NAME=${i#-*=}
            ;;
        -bm | --bind_mount)
            BIND_FLG=1
            ;;
        -h | --help)
            show_help
            exit 0
            ;;
        *)
            echo "unknown option '$i'"
            show_help
            exit 1
            ;;
    esac
done

SDK_CONTAINER=$(docker container ls -a --filter='NAME=sdk_for_ubuntu' | grep -cw sdk_for_ubuntu)
if [ 0 -ne ${SDK_CONTAINER} ]; then
    read -p "The container \"sdk_for_ubuntu\" already exists. Do you want to delete it before create new one? [y/n]: " yn
    case "$yn" in
        [Yy])
            docker stop sdk_for_ubuntu
            docker rm sdk_for_ubuntu
            ;;
        *)
            exit 0
            ;;
    esac
fi

SDK_CONTAINER=$(docker container ls -a --filter='NAME=sdk_for_ubuntu' | grep -cw sdk_for_ubuntu)

if [ ! -z ${CONTAINER_IP} ]; then
    docker network inspect ${DOCKER_NETWORK_NAME} &> /dev/null
    ret=$?
    if [ 0 -ne ${ret} ]; then
        echo "Create docker network. name=${DOCKER_NETWORK_NAME}, subnet=${SUBNET}, network bridge =${BRIDGE_NAME}"
        docker network create ${NETWORK_BN} --subnet=${SUBNET} ${DOCKER_NETWORK_NAME}
        ret=$?
    fi
fi

if [ 0 -eq ${ret} ]; then
### Create a docker container with volume_control ###
    if [ 0 -eq ${BIND_FLG} ]; then
        if [ ! -z ${CONTAINER_IP} ]; then
            NETWORK_OPT=" --network=${DOCKER_NETWORK_NAME} --ip=${CONTAINER_IP}"
        fi
        docker create -it --name sdk_for_ubuntu \
            --env LOCAL_UID=$(id -u) \
            --env LOCAL_GID=$(id -g) \
            --env LOCAL_USER=$(id -nu) \
            --hostname sdk \
            ${NETWORK_OPT} \
            ${AUBIST_AP_DOCKER_IMAGE_REPOSITORY}:${AUBIST_AP_DOCKER_IMAGE_TAG}
        ret=$?
        if [ 0 -eq ${ret} ]; then
            docker start sdk_for_ubuntu
            docker exec sdk_for_ubuntu /bin/bash -c "mkdir -p /ap/fc"
            docker exec sdk_for_ubuntu /bin/bash -c "mkdir -p /ap/app"
            docker cp ${AUBIST_AP_INSTALL_PATH}/ap_script sdk_for_ubuntu:/ap
            docker cp ${AUBIST_AP_INSTALL_PATH}/functional_cluster/. sdk_for_ubuntu:/ap/fc
            if "${SAMPLE_INSTALL_FLAG}"; then
                docker cp ${AUBIST_AP_INSTALL_PATH}/adaptive_application/. sdk_for_ubuntu:/ap/app
            fi
        fi
### Create a docker container with Bind Mount ###
    else
        if [ ! -d ${AUBIST_AP_INSTALL_PATH}/sdk_storage ]; then
            echo "'${AUBIST_AP_INSTALL_PATH}/sdk_storage folder' does not exist, reinstall AUBIST AP"
            exit -1
        fi
        if "${SAMPLE_INSTALL_FLAG}"; then
            if [ ! -d ${AUBIST_AP_INSTALL_PATH}/sdk_storage/applications ]; then
                ln -s ${AUBIST_AP_INSTALL_PATH}/adaptive_application ${AUBIST_AP_INSTALL_PATH}/sdk_storage/applications
            fi
        fi
        if [ ! -d ${AUBIST_AP_INSTALL_PATH}/sdk_storage/functional_cluster ]; then
            ln -s ${AUBIST_AP_INSTALL_PATH}/functional_cluster   ${AUBIST_AP_INSTALL_PATH}/sdk_storage/functional_cluster
        fi

        if [ 0 -eq ${ret} ]; then
            if [ ! -z ${CONTAINER_IP} ]; then
                NETWORK_OPT=" --network=${DOCKER_NETWORK_NAME} --ip=${CONTAINER_IP}"
            fi
            docker create -it --name sdk_for_ubuntu \
                --env LOCAL_UID=$(id -u) \
                --env LOCAL_GID=$(id -g) \
                --env LOCAL_USER=$(id -nu) \
                --volume ${AUBIST_AP_INSTALL_PATH}/sdk_storage/applications:/ap/app \
                --volume ${AUBIST_AP_INSTALL_PATH}/sdk_storage/functional_cluster:/ap/fc \
                --volume ${AUBIST_AP_INSTALL_PATH}/ap_script:/ap/ap_script \
                --hostname sdk \
                ${NETWORK_OPT} \
                ${AUBIST_AP_DOCKER_IMAGE_REPOSITORY}:${AUBIST_AP_DOCKER_IMAGE_TAG}
            ret=$?
            if [ 0 -eq ${ret} ]; then
                docker start sdk_for_ubuntu
            fi
        fi
    fi
fi

if [ 0 -eq ${ret} ]; then
    echo -e "\033[35m ### $(basename $0) finished. ###\033[m"
else
    echo -e "\033[35m ### $(basename $0) finished with error(ret=${ret}). ###\033[m"
fi

exit ${ret}

